<template>
  <div class="app-container calendar-list-container">
    <div v-if="!opShow">
      <search :list="queryList" :queryEntity="queryEntity"></search>
      <buttons :showQuery="true" :showBack="false" @query="doQuery" :list="btnList" :disabled="disabled" placeholder="订单号" :permissions="permissions" @add="doAdd"></buttons>
      <grid :columns="columns" :multi="false" :url="queryUrl" ref="mainGrid" height="auto" diffHeight="125" @change="rowCilck" :queryEntity="queryEntity" :pagination="true"
        @room="doRoom" @purchase="doPurchase" :permissions="permissions" @edit="doEdit" @view="doView" @pass="doPass" @prod="doProd" @delete="doDelete" @finish="doFinish"
        @cancel="doCancel" @title="doTitle" @step="doStep" @send="doSend"></grid>
    </div>
  </div>
</template>

<script>
import request from '@/router/axios'
import url from '@/const/apiURL'
import grid from '@/components/Grid'
import buttons from '@/components/Buttons'
import { Message, MessageBox } from 'element-ui'
import { mapGetters } from 'vuex'
import { YesOrNoFormatter, OrderStatusFormatter } from '@/const/dict'
import search from '@/components/Search'
import { colunms } from '@/const/colunms'
import { dict } from '@/const/dict'
import { bgColor } from '@/const/bgColor'
import { queryField } from '@/const/query'
import { getLodop } from '@/const/lodop'


export default {
  components: { grid, buttons, search },
  data () {
    return {
      queryUrl: url.ORDER_LIST_URL,
      disabled: true,
      opShow: false,
      queryEntity: { includeStatus: ["CHECKING", "PROD", "PASS", "FINISH"] },
      columns: [
        {
          label: '操作', width: 340, prop: 'op', align: 'center', template: 'op', opList: [
            { name: '生产', clickName: 'room', auth: 'order_room', valueProp: "orderStatus", showValue: ["PROD"] },
            { name: '批量生产', clickName: 'step', auth: 'order_step', valueProp: "orderStatus", showValue: ["PROD"] },
            { name: '采购', clickName: 'purchase', auth: 'order_purchase', valueProp: "orderStatus", showValue: ["PASS", "PROD"] },
            { name: '编辑', clickName: 'edit', auth: 'order_edit', valueProp: "orderStatus", showValue: ["CHECKING", "PASS", "PROD"] },
            { name: '查看', clickName: 'view', auth: 'order_view' },
            { name: '通过', clickName: 'pass', auth: 'order_pass', confirm: true, valueProp: "orderStatus", showValue: ["CHECKING"] },
            { name: '排期', clickName: 'prod', auth: 'order_prod', confirm: true, valueProp: "orderStatus", showValue: ["PASS"] },
            { name: '唛头', clickName: 'title', auth: 'yes' },
            { name: '完成', clickName: 'finish', auth: 'order_finish', confirm: true, valueProp: "orderStatus", showValue: ["PROD"], btnColor: 'green' },
            { name: '取消', clickName: 'cancel', auth: 'order_cancel', confirm: true, valueProp: "orderStatus", showValue: ["PASS", "PROD"], btnColor: 'red' },
            { name: '删除', clickName: 'delete', auth: 'order_delete', confirm: true, valueProp: "orderStatus", showValue: ["CHECKING"], btnColor: 'red' },
            { name: '发货', clickName: 'send', auth: 'yes' },
          ]
        },
        { label: '大货订单', width: 180, prop: 'orderNo', align: 'left' },
        { label: '客户名称', width: 200, prop: 'customerName', align: 'left' },
        { label: '币种', width: 80, prop: 'currency', align: 'center' },
        { label: '数量', width: 100, prop: 'quantity', align: 'center' },
        { label: '总金额', width: 100, prop: 'totalAmount', align: 'center' },
        { label: '订单状态', width: 100, prop: 'orderStatus', align: 'center', formatter: OrderStatusFormatter, template: 'bgColor', bgColor: bgColor().OrderStatus },
        { label: '发货时间', width: 100, prop: 'sendTime', align: 'center' },
        { label: '创建时间', width: 100, prop: 'createTime', align: 'center' }
      ],
      btnList: [
        { name: '新增', type: 'primary', icon: 'edit', click: 'add', auth: 'order_add', ctr: false },
        // { name: '编辑', type: 'primary', icon: 'edit', click: 'edit', auth: 'order_edit', ctr: true },
        //  { name: '查看', type: 'primary', icon: 'edit', click: 'view', auth: 'order_view', ctr: true },
        // { name: '删除', type: 'primary', icon: 'edit', click: 'delete', auth: 'order_delete', ctr: true, confirm: true },
        //  { name: '通过', type: 'primary', icon: 'edit', click: 'pass', auth: 'order_pass', ctr: true, confirm: true },
        //  { name: '排期', type: 'primary', icon: 'edit', click: 'prod', auth: 'order_prod', ctr: true, confirm: true },
        //  { name: '完成', type: 'primary', icon: 'edit', click: 'finish', auth: 'order_finish', ctr: true, confirm: true },
        //   { name: '工序', type: 'primary', icon: 'edit', click: 'step', auth: 'order_step', ctr: true }
      ],
      queryList: [
        { name: '大货订单', type: 'text', prop: 'orderNoLike', placeholder: "大货订单" },
        { name: '创建时间', type: 'date', prop: 'startTime', placeholder: "" },
        { name: '----', type: 'date', prop: 'endTime', placeholder: "", },
        { name: '订单状态', type: 'select', prop: 'orderStatus', placeholder: "订单状态", values: dict().OrderStatus, label: 'name', value: 'value' },
        { name: '客户名称', type: 'query', prop: 'customerCode', rowValue: 'customerCode', rowName: 'customerName', url: url.CUSTOMER_LIST_URL, columns: colunms().customerColumns, pagination: true, queryList: queryField().customer, queryEntity: { customerType: 'CUSTOMER' } }
      ]
    }
  },
  created () {
    this.initLodop()
  },
  computed: {
    ...mapGetters(['permissions'])
  },
  methods: {
    doQuery (text) {
      this.$refs['mainGrid'].doQuery(text)
    },
    rowCilck (row) {
      this.disabled = row.length == 0
    },
    cancel () {
      this.opShow = false
    },
    initLodop () {
      LODOP = getLodop()
      LODOP.PRINT_INIT('') //打印初始化
      LODOP.SET_PRINT_STYLE('FontSize', 18) //设置对象风格
      LODOP.SET_PRINT_STYLE('Bold', 1) ////设置对象风格
      LODOP.SET_PRINT_PAGESIZE(0, 2000, 2000, '') //设定纸张大小
      LODOP.SET_PRINT_MODE('PRINT_PAGE_PERCENT', '55%')//设置缩放
      LODOP.SET_PRINT_MODE("PROGRAM_CONTENT_BYVAR", true);
      LODOP.SET_PREVIEW_WINDOW(2, 2, 0, 0, 0, '')//设置窗口
    },
    doTitle (row) {
      //  const row = this.$refs.mainGrid.getSelectOne()
      let _this = this
      if (row) {
        this.viewOp(row.orderNo).then((data) => {
          if (data) {
            if (data.template) {
              eval(data.template)
            }
            LODOP.PRINT_DESIGN() //打印设计
            LODOP.On_Return = function (TaskID, value) {
              _this.$confirm("是否保存模板?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
              }).then(() => {
                _this.saveTemplate({ customerCode: row.orderNo, template: value })
              });
            }
          }
        })
      }
    },
    doSend(row){
       this.$router.push({ path: '/prod/order/send', query: { orderNo: row.orderNo } })
    },
    doStep (row) {
      this.$router.push({ path: '/prod/room/step/order', query: { orderNo: row.orderNo } })
    },
    doRoom (row) {
      this.$router.push({ path: '/prod/room/new', query: { orderNo: row.orderNo } })
    },
    doPurchase (row) {
      this.$router.push({ path: '/prod/purchase/new', query: { businessOrderNo: row.orderNo } })
    },
    doAdd () {
      this.$router.push({ path: '/prod/order/add', query: {} })
    },
    doView (row) {
      // const row = this.$refs['mainGrid'].getSelectOne()
      if (row) {
        this.$router.push({
          path: '/prod/order/view',
          query: { orderNo: row.orderNo }
        })
      }
    },
    doEdit (row) {
      // const row = this.$refs['mainGrid'].getSelectOne()
      if (row) {
        this.$router.push({
          path: '/prod/order/edit',
          query: { orderNo: row.orderNo }
        })
      }
    },
    doCancel (row) {
      // const row = this.$refs['mainGrid'].getSelectOne()
      if (row) {
        this.cancelOp(row.orderNo).then(data => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    doPass (row) {
      // const row = this.$refs['mainGrid'].getSelectOne()
      if (row) {
        this.passOp(row.orderNo).then(data => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    saveTemplate (entity) {
      this.addTemolateOp(entity).then((data) => {
        if (data) {
          this.$refs['mainGrid'].reload()
        }
      })
    },
    addTemolateOp (data) {
      return request({
        url: url.CUSTOMER_TEMPLATE_ADD_URL,
        method: 'post',
        data: data
      })
    },
    viewOp (code) {
      return request({
        url: url.CUSTOMER_TEMPLATE_VIEW_URL,
        method: 'post',
        data: { 'key': 'key' },
        params: { 'customerCode': code }
      })
    },
    cancelOp (orderNo) {
      return request({
        url: url.ORDER_CANCEL_URL,
        method: 'post',
        data: { key: 'key' },
        params: { orderNo: orderNo }
      })
    },
    deleteOp (orderNo) {
      return request({
        url: url.ORDER_DELETE_URL,
        method: 'post',
        data: { key: 'key' },
        params: { orderNo: orderNo }
      })
    },
    doDelete (row) {
      //const row = this.$refs['mainGrid'].getSelectOne()
      if (row) {
        this.deleteOp(row.orderNo).then(data => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    passOp (orderNo) {
      return request({
        url: url.ORDER_PASS_URL,
        method: 'post',
        data: { key: 'key' },
        params: { orderNo: orderNo }
      })
    },
    finishOp (orderNo) {
      return request({
        url: url.ORDER_FINISH_URL,
        method: 'post',
        data: { key: 'key' },
        params: { orderNo: orderNo }
      })
    },
    doFinish (row) {
      // const row = this.$refs.mainGrid.getSelectOne()
      if (row) {
        this.finishOp(row.orderNo).then(data => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    },
    prodOp (orderNo) {
      return request({
        url: url.ORDER_OPEN_URL,
        method: 'post',
        data: { key: 'key' },
        params: { orderNo: orderNo }
      })
    },
    doProd (row) {
      //const row = this.$refs.mainGrid.getSelectOne()
      if (row) {
        this.prodOp(row.orderNo).then(data => {
          if (data) {
            this.$refs['mainGrid'].reload()
          }
        })
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
</style>
